package com.tinem.platform.web.auth.login.oauth2.ext;

import com.tinem.platform.web.auth.page.LoginUserInfo;
import lombok.Getter;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.SpringSecurityCoreVersion;

import java.util.Collection;

/**
 * @author fzh
 */
@Getter
public class OAuth2AuthenticationToken extends AbstractAuthenticationToken {

	private static final long serialVersionUID = SpringSecurityCoreVersion.SERIAL_VERSION_UID;

	private final String principal;
	private LoginUserInfo loginUserInfo;

	public OAuth2AuthenticationToken(String userId) {
		super(null);
		this.principal = userId;
		setAuthenticated(false);
	}

	public OAuth2AuthenticationToken(String to, LoginUserInfo loginUserInfo, Collection<? extends GrantedAuthority> authorities) {
		super(authorities);
		this.principal = to;
		this.loginUserInfo = loginUserInfo;
		super.setAuthenticated(true);
	}
	
	@Override
	public Object getCredentials() {
		return loginUserInfo.getPassword();
	}

	@Override
	public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
		super.setAuthenticated(isAuthenticated
		);
	}

	@Override
	public void eraseCredentials() {
		super.eraseCredentials();
	}

}